﻿using UnityEngine;
using System.Diagnostics;
using UnityEngine.SceneManagement;

namespace SoarD.Pandora
{
    public class PdrAnalyzer : MonoBehaviour
    {
        public static float DeviceScore;//设备性能评分
        private Mesh _Mesh;
        private Material _Mat;
        private int _State;
        private int _FrameCount;
        private int _TotalDrawTimes;
        private long _TotalDrawMilliseconds;
        private const int SAMPLE_COUNT = 30;
        private const string FIRST_SCENE = "GameMain";

        public static void CaclScore()
        {
            GameObject uicamera = GameObject.Find("GameMain/UICamera");
            if (uicamera != null)
            {
                PdrAnalyzer analyzer = uicamera.AddComponent<PdrAnalyzer>();
                analyzer.Init();
            }
        }

        public void Init()
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            _Mesh = sphere.GetComponent<MeshFilter>().mesh;
            _Mat = sphere.GetComponent<MeshRenderer>().material;
            DestroyImmediate(sphere);
        }

        public void OnPostRender()
        {
            switch (_State)
            {
                case 0:
                    {
                        if (!FIRST_SCENE.Equals(SceneManager.GetActiveScene().name))
                        {
                            UnityEngine.Debug.LogFormat("Analyzer Begin----------------");
                            _State = 1;
                        }
                    }
                    break;
                case 1:
                    {
                        _Mat.SetPass(0);

                        int drawTimes = 0;
                        long milliseconds = 0;

                        Stopwatch sw = new Stopwatch();
                        for (int n = 0; n < 1000; ++n)
                        {
                            sw.Start();
                            for (int i = 0; i < 100; ++i)
                            {
                                Graphics.DrawMeshNow(_Mesh, Vector3.zero, Quaternion.identity);
                                drawTimes++;
                            }
                            sw.Stop();

                            milliseconds += sw.ElapsedMilliseconds;
                            if (milliseconds > 10)
                                break;
                        }

                        _TotalDrawTimes += drawTimes;
                        _TotalDrawMilliseconds += milliseconds;

                        DeviceScore = 1.0f * _TotalDrawTimes / _TotalDrawMilliseconds;

                        _FrameCount++;
                        if (_FrameCount >= SAMPLE_COUNT)// 采样N帧数据
                        {
                            UnityEngine.Debug.LogFormat("Analyzer Finish! draw mesh time(MS): {0}, draw times: {1}, score: {2}", _TotalDrawMilliseconds / SAMPLE_COUNT, _TotalDrawTimes / SAMPLE_COUNT, DeviceScore);
                            _State = 2;
                        }
                    }
                    break;
                case 2:
                    {
                        DestroyImmediate(this);
                    }
                    break;
            }
        }
    }
}